package com.xm.xmapp.fragment;

import java.util.ArrayList;

import android.view.View;
import android.widget.RelativeLayout;

import com.android.volley.VolleyError;
import com.xm.xmapp.R;
import com.xm.xmapp.adapter.YHQAdapter;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.customview.XListView;
import com.xm.xmapp.customview.XListView.IXListViewListener;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Get_user_coupons_listRequest;
import com.xm.xmapp.response.bean.ResponseUserCouponsList;

public class YHQUseFragment extends BaseFragment {

	private XListView xListView;
	private RelativeLayout rel;
	private Get_user_coupons_listRequest requestBean = new Get_user_coupons_listRequest();
	private int type;
	private int page;
	private YHQAdapter adapter;
	private ArrayList<ResponseUserCouponsList> list = new ArrayList<ResponseUserCouponsList>();

	public YHQUseFragment(int type) {
		this.type = type;
	}

	@Override
	public int setContentView() {
	
		return R.layout.fragment_yhq;
	}

	public YHQUseFragment() {
		super();
		
	}

	@Override
	public void initView() {
		
		xListView = (XListView) findViewById(R.id.xListView);
		rel = (RelativeLayout) findViewById(R.id.rel);
	}

	@Override
	public void initData() {

		page = 1;
		requestBean.pagesize = CodeConstant.pageSize;
		requestBean.type = type;
		promptDialog.show();
		xListView.setPullRefreshEnable(true);
		xListView.setPullLoadEnable(true);

		for (int i = 0; i < 5; i++) {
			ResponseUserCouponsList bean = new ResponseUserCouponsList();
			list.add(bean);
		}

		adapter = new YHQAdapter(list, getActivity(), R.layout.item_fragment_yhq);
		xListView.setAdapter(adapter);

		xListView.setVisibility(View.VISIBLE);
		rel.setVisibility(View.GONE);
		getData();
	}

	private void getData() {

		requestBean.page = page;
		MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean,
				new GetDataInterFace<String>() {

					@Override
					public void onResponse(String response) {
				
						xListView.stopRefresh();
						xListView.stopLoadMore();
						promptDialog.dismiss();
					}
				}, new DisssmissInterFace() {

					@Override
					public void putError(VolleyError error) {
					
						xListView.stopRefresh();
						xListView.stopLoadMore();
						promptDialog.dismiss();
					}
				});

	}

	@Override
	public void initEvent() {

		xListView.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				page = 1;
				getData();
			}

			@Override
			public void onLoadMore() {
				page++;
				getData();
			}
		});
	}

}
